package com.asion.gankapp.view.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.AppCompatButton;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.asion.gankapp.R;
import com.asion.gankapp.applacation.MyApp;
import com.asion.gankapp.utils.ThemeChangeUtil;

import org.greenrobot.greendao.query.Query;

import gl.com.app.User;
import gl.com.app.UserDao;

import static com.asion.gankapp.R.id.input_name;
import static com.asion.gankapp.R.id.input_password;

public class SignupActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText inputName;
    private EditText inputPassword;
    private AppCompatButton btnSignup;
    private TextView linkLogin;
    private String name;
    private String password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ThemeChangeUtil.changeTheme(this);
        setContentView(R.layout.activity_signup);
        initView();

        btnSignup.setOnClickListener(this);
        linkLogin.setOnClickListener(this);
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_signup:
                signup();
                break;
            case R.id.link_login:
                finish();
                break;
        }
    }

    public void signup() {
        if (!validate()) {
            onSignupFailed();
            return;
        }

        btnSignup.setEnabled(false);
        name = inputName.getText().toString();
        password = inputPassword.getText().toString();

        Query<User> query = MyApp.getUserDao().queryBuilder().where(UserDao.Properties.Name.eq(name)).build();
        if(query.list().size() == 0){
            User user = new User(null,name,password,null);
            MyApp.getUserDao().insert(user);
            onSignupSuccess();
        }else{
            Toast.makeText(this, "用户名重复，请重新命名！", Toast.LENGTH_SHORT).show();
            btnSignup.setEnabled(true);
        }
    }


    @Override
    public void onBackPressed() {
        finish();
    }

    public void onSignupSuccess() {
        btnSignup.setEnabled(true);
        Intent intent = new Intent();
        intent.putExtra("name",name);
        intent.putExtra("password",password);
        setResult(RESULT_OK, intent);
        finish();
    }

    public void onSignupFailed() {
        Toast.makeText(this, "登录失败，请重新检查", Toast.LENGTH_LONG).show();
        btnSignup.setEnabled(true);
    }

    public boolean validate() {
        boolean valid = true;

        String name = inputName.getText().toString();
        String password = inputPassword.getText().toString();

        if (name.isEmpty() || name.length() < 3) {
            inputName.setError("至少得有三个字母");
            valid = false;
        } else {
            inputName.setError(null);
        }

        if (password.isEmpty() || password.length() < 4 || password.length() > 13) {
            inputPassword.setError("密码必须位于4-13位之间");
            valid = false;
        } else {
            inputPassword.setError(null);
        }

        return valid;
    }

    private void initView() {
        inputName = (EditText) findViewById(input_name);
        inputPassword = (EditText) findViewById(input_password);
        btnSignup = (AppCompatButton) findViewById(R.id.btn_signup);
        linkLogin = (TextView) findViewById(R.id.link_login);

        btnSignup.setOnClickListener(this);
    }
}
